package com.zcj.test.guide;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by binIoter
 */
public class Common {
    /**
     * 设置Component
     */
    static View componentToView(LayoutInflater inflater, Component c) {
        View view = c.getView(inflater);
        final MaskView.LayoutParams lp = new MaskView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.offsetX = c.getXOffset();
//    lp.offsetY = c.getYOffset();
        lp.offsetY = c.getYOffset();
        lp.targetAnchor = c.getAnchor();
        lp.targetParentPosition = c.getFitPosition();
        view.setLayoutParams(lp);
        return view;
    }

    /**
     * Rect在屏幕上去掉状态栏高度的绝对位置
     */
    static Rect getViewAbsRect(View view, int parentX, int parentY) {
        int[] loc = new int[2];
        view.getLocationInWindow(loc);
        Rect rect = new Rect();
        rect.set(loc[0], loc[1], loc[0] + view.getMeasuredWidth(), loc[1] + view.getMeasuredHeight());
        rect.offset(-parentX, -parentY);
        return rect;
    }

    /**
     * Rect在屏幕上去掉状态栏高度的绝对位置
     */
    static Rect getViewAbsRect(View view, int parentX, int parentY, Component mComponent) {
        int[] loc = new int[2];
        view.getLocationInWindow(loc);
        Rect rect = new Rect();
        rect.set(loc[0], loc[1] - (mComponent.getScrollViewYOffset() == 0 ? 0 : mComponent.getScrollViewYOffset()), loc[0] + view.getMeasuredWidth(), loc[1] + view.getMeasuredHeight() - (mComponent.getScrollViewYOffset() == 0 ? 0 : mComponent.getScrollViewYOffset()));
        rect.offset(-parentX, -parentY);
        return rect;
    }

    public static boolean setFirstStart(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("first", Context
                .MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("isFirstStart", false);
        return editor.commit();
    }

    public static boolean isFirstStart(Context context) {
        SharedPreferences preferences;
        preferences = context.getSharedPreferences("first", 0);
        return preferences.getBoolean("isFirstStart", true);
    }

    public static boolean setFirstInstall(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("first", Context
                .MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("isFirstInstall", false);
        editor.putLong("firstInstalledTime", System.currentTimeMillis());
        return editor.commit();
    }

    public static boolean isFirstInstall(Context context) {
        SharedPreferences preferences;
        preferences = context.getSharedPreferences("first", 0);
        return preferences.getBoolean("isFirstInstall", true);
    }

    /**
     * 是否是新用户
     *
     * @param context context
     * @return 0 老用户 1 新用户
     */
    public static int isNewUser(Context context) {
        SharedPreferences preferences;
        preferences = context.getSharedPreferences("first", 0);
        long installedTime = preferences.getLong("firstInstalledTime", 0);
        if (System.currentTimeMillis() - installedTime < 259200000) {
            return 1;
        } else {
            return 0;
        }
    }

    public static boolean setTopicId(Context context, String topic_id) {
        SharedPreferences preferences = context.getSharedPreferences("topic", Context
                .MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        String topic_ids;
        if (preferences.contains("topic_id")) {
            topic_ids = preferences.getString("topic_id", "");
            topic_ids += "," + topic_id;
        } else {
            topic_ids = topic_id;
        }
        editor.putString("topic_id", topic_ids);
        return editor.commit();
    }

    public static String[] getTopicIds(Context context) {
        SharedPreferences preferences;
        preferences = context.getSharedPreferences("topic", 0);
        String topic = preferences.getString("topic_id", "");
        return convertStrToArray(topic);
    }

    //使用String的split 方法
    private static String[] convertStrToArray(String str) {
        String[] strArray;
        strArray = str.split(","); //拆分字符为"," ,然后把结果交给数组strArray
        return strArray;
    }
}
